package com.smf.lsc.authorization.security.service.jwt;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import com.alibaba.fastjson.JSON;

/**
 * 向JWT中添加额外信息
 * Created on 2021/11/19.
 *
 * @author ln
 * @since 1.0
 */
public class MyJwtTokenEnhancer implements TokenEnhancer {
    
	@Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> info = new HashMap<>();
        info.put("userinfo", JSON.toJSONString(authentication.getPrincipal()));
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
        return accessToken;
    }
}
